<template>
  <div class="app-container">

    <u-page title="车位管理">
      <div slot="header">
        <el-button type="primary" icon="el-icon-edit" @click="hdlOpen(newForm)">
          {{ textMap['create'] }}
        </el-button>
      </div>

      <u-filtered>
        <el-form ref="listQuery" :inline="true" :model="listQuery" class="search-form">
          <el-form-item label="车位" prop="spaceNo">
            <el-input v-model="listQuery.spaceNo" placeholder="关键词查询" clearable />
          </el-form-item>
          <el-form-item label="分配" prop="allocated">
            <el-select v-model="listQuery.allocated" placeholder="分配" clearable>
              <el-option label="全部" value="" />
              <el-option label="已分配" :value="true" />
              <el-option label="未分配" :value="false" />
            </el-select>
          </el-form-item>
          <el-form-item label="状态" prop="status">
            <u-status v-model="listQuery.status" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" @click="hdlFilter">搜索</el-button>
          </el-form-item>
          <el-form-item>
            <el-dropdown>
              <el-button type="primary">
                批量操作<i class="el-icon-arrow-down el-icon--right" />
              </el-button>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item :disabled="selectData.length===0" @click.native="hdlDel">批量删除</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </el-form-item>
        </el-form>
      </u-filtered>

      <u-table
        :data="list"
        :loading="listLoading"
        :options="listOptions"
        :columns="columns"
        :pagination.sync="listQuery"
        :fetch="hdlList"
        @selection-change="hdlSelectChange"
      >
        <template v-slot:right>
          <el-table-column label="操作" align="center" fixed="right">
            <template slot-scope="{ row }">
              <el-button type="text" @click="hdlEdit(row)">编辑</el-button>
              <el-button type="text" @click="hdlDelete(row.id, row.spaceNo)">删除</el-button>
            </template>
          </el-table-column>
        </template>
      </u-table>

    </u-page>

    <u-dialog :title="textMap[dialogStatus]" :show.sync="dialogFormVisible" @confirm="dialogStatus==='create'?hdlCreate():hdlUpdate()">
      <el-form ref="dialogForm" :rules="rules" :model="dataForm" label-width="60px">

        <el-form-item prop="spaceNo" label="车位">
          <el-input v-model="dataForm.spaceNo" placeholder="车位编号" clearable maxlength="30" show-word-limit />
        </el-form-item>

        <el-form-item prop="sort" label="排序">
          <el-input v-model="dataForm.sort" placeholder="数字越大，排名越靠前" clearable />
        </el-form-item>

        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="dataForm.status">
            <el-radio :label="1">启用</el-radio>
            <el-radio :label="0">禁用</el-radio>
          </el-radio-group>
        </el-form-item>

      </el-form>
    </u-dialog>

  </div>
</template>
<script>
export default {
  data() {
    return {
      api: this.$u.api.PluginsParkingSpace,
      // 表格
      columns: [
        {
          prop: 'spaceNo',
          label: '车位'
        },
        {
          prop: 'sort',
          label: '排序'
        },
        {
          prop: 'allocated',
          label: '分配',
          render: (h, params) => {
            const text = params.row.allocated ? '已分配' : '未分配';
            return h('div', text);
          }
        },
        {
          prop: 'status',
          label: '状态',
          render: (h, params) => {
            const statusText = params.row.status === 1 ? '已启用' : '禁用';
            const tagType = params.row.status === 1 ? 'success' : 'danger';
            return h('el-tag', { props: { type: tagType }}, statusText);
          }
        },
        {
          prop: 'createdAt',
          label: '创建时间',
          timestamp: true
        }
      ],

      list: [],
      listLoading: true,
      listOptions: {
        mutiSelect: true, // boolean 是否多选
        stripe: true // boolean 斑马纹
      },
      listQuery: {
        pageNumber: 1,
        pageSize: 10,
        totalCount: 1,
        allocated: '',
        spaceNo: '',
        status: ''
      },

      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        update: '编辑车位',
        create: '新增车位'
      },
      dataForm: {},
      rules: {
        spaceNo: [
          { required: true, message: '请输入车位编号', trigger: ['blur'] }
        ]
      },
      // 删除选中数据
      selectData: [],
      newForm: {
        status: 1
      }
    }
  },
  created() {
    this.hdlList()
  }
}
</script>
